#include "rtmpreceive.h"



RtmpReceive::RtmpReceive()
    :isExit(false)    
{

   mXV= XReceiveVideo::Get();
   mXV->SetEvent(this);
}

RtmpReceive::~RtmpReceive()
{

}

void RtmpReceive::StopAll()
{
    mXV->Stop();
    isExit = true;
}

void RtmpReceive::run()
{
    qDebug() << "receive rtmp: " << mRtmpUrl.c_str() << endl;

     if (!mXV->Init(mRtmpUrl.c_str()))
     {
         cout << "open receive rtmp failed!" << endl;
         return;
     }
     cout << "open receive rtmp success!" << endl;
     mXV->start();


}

void RtmpReceive::OnReceives(QImage image)
{
    if(mEvent != NULL)
    {
        mEvent->OnReceiveVideo(image);
    }
}

QImage RtmpReceive::matToqimage(const cv::Mat& mat)
{
    QImage img;
    int chana = mat.channels();
    //依据通道数不同，改变不同的装换方式
    if(3 == chana ){
        //调整通道次序
        //cvtColor(mat,mat,CV_BGR2RGB);
        img = QImage(static_cast<uchar *>(mat.data),mat.cols,mat.rows,QImage::Format_RGB888);
    }
    else if(4 == chana )
    {
        //argb
        img = QImage(static_cast<uchar *>(mat.data),mat.cols,mat.rows,QImage::Format_ARGB32);
    }
    else {
    //单通道，灰度图
    img = QImage( mat.cols, mat.rows , QImage::Format_Indexed8);
    uchar * matdata = mat.data;
        for(int row = 0 ; row <mat.rows ; ++row )
        {
            uchar* rowdata = img.scanLine( row );
            memcpy(rowdata,matdata ,mat.cols);
            matdata+=mat.cols;
        }
    }
    return img;
}
